import { tool } from "@/lib";
import { useLogin } from "@/hooks";
import { useMyStore } from "@/stores";

type Args = {
    title: string
    path: string
    imageUrl: string
    params: Record<string, unknown>
}

/**分享hook */
export const useShare = (args: Args) => {
    args = args || {
        title: '这是一个分享页面',
        path: '/pages/index/index',
        imageUrl: '',
        params: {}
    };
    if (typeof args.params === 'undefined') {
        args.params = {};
    }
    let user_id = '0';
    const { isLogin } = useLogin()
    const { info } = storeToRefs(useMyStore())
    if (isLogin) {
        user_id = info.value.id
    }
    if (typeof args.path === 'undefined' || (args.path === '/pages/index/index' && typeof args.params.page_id === 'undefined')) {
        args.path = tool.createUrl(`/pages/index/index?user_id=${user_id}`, args.params);
    } else {
        args.params.path = args.path;
        args.params.user_id = user_id;
        // #ifdef MP
        args.path = `/pages/index/index?scene=share&user_id=${user_id}&params=${JSON.stringify(args.params)}`;
        // #endif
        // #ifdef H5
        args.path = `/pages/index/index?scene=share&user_id=${user_id}&params=${btoa(JSON.stringify(args.params))}`;
        // #endif
    }
    return {
        args
    }
}